package com.hr.controller;

import com.hr.dto.Page;
import com.hr.dto.Result;
import com.hr.entity.Book;
import com.hr.service.BookService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class BookController {

    @Resource
    private BookService bookService;

    @PostMapping("/addBook")
    public Result addBook(@RequestBody Book book){
        bookService.addBook(book);
        return Result.ok();
    }

    @GetMapping("/deletaBookByBookId")
    private Result deletaBookByBookId(Integer bookId){
        bookService.deletaBookByBookId(bookId);
        return Result.ok();
    }

    @PostMapping("/batchDeleteBook")
    public Result batchDeleteBook(@RequestBody List<Integer> bookIds){
        bookService.batchDeleteBook(bookIds);
        return Result.ok();
    }

    @PostMapping("/updateBook")
    public Result updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        return Result.ok();
    }

    @GetMapping("/getBooksByParams")
    public Result getBooksByParams(@RequestParam("pageNo") Integer pageNo,
                                   @RequestParam("pageSize") Integer pageSize,
                                   String bookName,
                                   Integer categoryId,
                                   Integer status,
                                   @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date start,
                                   @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date end){
        Page page = bookService.getBooksByParams(pageNo, pageSize, bookName, categoryId, status, start, end);
        return Result.ok(page);
    }

}
